package com.lcredu.web.requestParam;

import com.lcredu.web.requestParam.entity.Master;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author 黎长容
 * @date 2025-07-21 10:39
 */
@RequestMapping("/vote")
@Controller
public class VoteHandler {
    /**
     * value : 接受要提交的参数
     * required : 设置必要是否提交
     *
     * @param username
     */
    @RequestMapping("/getName")
    public String getName(@RequestParam(value = "name", required = false) String username) {
        System.out.println("接收到参数= " + username);
        return "success";
    }


    @RequestMapping("/vote02")
    public String test02(@RequestHeader("Accept-Encoding") String ae,
                         @RequestHeader("Host") String host) {
        System.out.println("Accept-Encoding= " + ae);
        System.out.println("Host= " + host);
        return "success";
    }


    //获取到添加的主人信息
    @RequestMapping(value = "/vote03")
    public String test03(Master master) {
        System.out.println("主人信息= " + master);
        //返回结果
        return "success";
    }


    /**
     * springmvc 会自动的将传入进来的数据封装为master对象,并且存入request域中
     * 存入的形式是(类名首字母小写,对象)
     *
     * @param master
     * @return
     */
    @RequestMapping("/vote04")
    public String test04(Master master, HttpServletRequest request) {
        System.out.println(master);
        //增加属性address
        request.setAttribute("address", "湖南");
        master.setName("修改后的宠物名");

        return "vote_ok";
    }

    /**
     * @param master  : springmvc会扫描map传入的值,然后放入request域中
     * @param hashmap
     * @return
     */
    @RequestMapping("/vote05")
    public String test05(Master master, Map<String, String> hashmap) {
        System.out.println(master);
        hashmap.put("address", "上海");
        //下面情况会覆盖
//        hashmap.put("master", null);
        return "vote_ok";
    }


    @RequestMapping("/vote06")
    public ModelAndView test06(Master master) {
        System.out.println("ok...test06");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("address", "呼伦贝尔");
        modelAndView.setViewName("vote_ok");
        return modelAndView;
    }

    //默认也会添加到request域中...
    @RequestMapping("/vote07")
    public String test07(Master master, HttpSession httpSession) {
        System.out.println("ok...test07");
        httpSession.setAttribute("master", master);
        httpSession.setAttribute("address", "湖南");
        return "vote_ok";
    }


    /**
     * 类似于aop的前置通知...
     * 可以用于判断用户信息
     */
    @ModelAttribute
    public void modelAttribute() {
        System.out.println("----modelAttribute前置方法被调用----");
    }
    
    


}
